package com.zpark.resume.service.impl;

import com.zpark.resume.entity.Project;
import com.zpark.resume.entity.Training;
import com.zpark.resume.exception.*;
import com.zpark.resume.mapper.ITrainingMapper;
import com.zpark.resume.service.ITrainingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @PackageName: com.zpark.resume.service.impl
 * @ClassName: TrainingServiceImpl
 * @Description:
 * @author: RZS
 * @date: 2023/6/1  22:11
 */
@Service
public class TrainingServiceImpl implements ITrainingService {

    @Autowired
    private ITrainingMapper trainingMapper;

    /**
     * 根据cv的id查询数据
     * @param cvId
     * @return
     */
    @Override
    public List<Training> findByCvId(Integer cvId) {
        // 调用持久层接口查询数据
        List<Training> trainings = trainingMapper.selectByCvId(cvId);
        // 判断查询结果
        if (trainings.size() <= 0) {
            // 结果为空，抛出异常FindCvByIdIsNullException("无效的ID")
            throw new FindCvByIdIsNullException("无效的ID");
        }
        return trainings;
    }

    @Override
    public Integer addTraining(Training training) {
        // 判断training是否为null
        if (training == null) {
            throw new InsertTrainingLoseException("添加培训经历失败");
        }
        // 调用持久层接口添加数据
        Integer row = trainingMapper.insertTraining(training);
        // 判断执行结果
        if (row <= 0) {
            throw new InsertTrainingLoseException("添加培训经历失败");
        }
        // 返回受影响的行数
        return row;
    }

    @Override
    public Integer changeTrainingById(Training training) {
        // 判断training是否为null
        if (training == null) {
            throw new UpdateTrainingLoseException("修改培训历失败");
        }
        // 调用持久层接口添加数据
        Integer row = trainingMapper.updateTrainingById(training);
        // 判断执行结果
        if (row <= 0) {
            throw new UpdateTrainingLoseException("修改培训经历失败");
        }
        // 返回受影响的行数
        return row;
    }

    @Override
    public Integer removeTrainingById(Integer id) {
        // 判断id是否为null
        if (id == null || id == 0) {
            throw new DeleteTrainingLoseException("删除培训经历失败");
        }
        // 调用持久层接口删除数据
        Integer row = trainingMapper.deleteTrainingById(id);
        // 判断执行结果
        if (row <= 0) {
            throw new DeleteTrainingLoseException("删除培训经历失败");
        }
        // 返回受影响的行数
        return row;
    }
}
